<?php

global $_W, $_GPC;
load()->func('tpl');
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($op == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $case = pdo_fetchall('select * from ' . tablename('franchise_decorate_case') . " where uniacid=:uniacid ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid']));
    foreach ($case as $k => $v) {
        $case[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
    }
    $total = pdo_fetchcolumn('select * from ' . tablename('franchise_decorate_case') . "where  uniacid=:uniacid ORDER BY id DESC", array(':uniacid' => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/franchise/decorate_case');
    exit;
}
if ($op == 'edit') {
    $id = intval($_GPC['id']);
    if (!empty($id)) {
        $case = pdo_fetch('SELECT * FROM ' . tablename('franchise_decorate_case') . ' WHERE id=:id and uniacid=:uniacid ', array(':uniacid' => $_W['uniacid'], ':id' => $id));
    }
    if (array_key_exists('submit', $_GPC)) {
        empty($_GPC['image']) && message('请选择案例图片');
        $data['uniacid'] = $_W['uniacid'];
        $data['image'] = $_GPC['image'];
        $data['status'] = $_GPC['status'];
        $data['description'] = htmlspecialchars_decode($_GPC['description']);
        if (!empty($id)) {
            pdo_update('franchise_decorate_case', $data, array('uniacid' => $_W['uniacid'], 'id' => $id));
        } else {
            $data['createtime'] = time();
            pdo_insert('franchise_decorate_case', $data);
        }
        message('装修案例保存成功', $this->createWebUrl('franchise/decorate_case'), 'success');
    }
    include $this->template('web/franchise/decorate_case');
    exit;
}
if ($op == 'status_set') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch('select * from ' . tablename('franchise_decorate_case') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($info)) {
        $status = $info['status'] == 1 ? '0' : '1';
        pdo_update("franchise_decorate_case", array('status' => $status), array("id" => $id, "uniacid" => $_W['uniacid']));
        die(json_encode(array("result" => 1, 'status' => $status)));
    } else {
        die(json_encode(array("result" => 0)));
    }
}
if ($op == 'need') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $need = pdo_fetchall('select n.*,m.avatar,m.nickname from ' . tablename('franchise_decorate_need') . "as n left join " . tablename('ewei_shop_member') . "as m on n.openid=m.openid where n.uniacid=:uniacid ORDER BY n.id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid']));
    foreach ($need as $k => $v) {
        $need[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        $need[$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
    }
    $total = pdo_fetchcolumn('select * from ' . tablename('franchise_decorate_need') . "where  uniacid=:uniacid ORDER BY id DESC", array(':uniacid' => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/franchise/decorate_case');
    exit;
}
if ($op == 'confirm') {
    $id = intval($_GPC['id']);
    $need = pdo_fetch('select * from ' . tablename('franchise_decorate_need') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($need)) {
        $data['status']=intval($_GPC['status']);
        if($data['status']==1){
        $data['maketime']=time();
        }else{
        $data['finishtime']=time();
        }
        pdo_update('franchise_decorate_need',$data, array('uniacid'=>$_W['uniacid'],'id'=>$id));
        message('需求状态更新成功', $this->createWebUrl('franchise/decorate_case',array('op'=>'need')), 'success');
    } else {
        message('需求不存在!', $this->createWebUrl('franchise/decorate_case',array('op'=>'need')), 'error');
    }
}